package com.lwlk.util.functional;

import com.lwlk.util.flow.Try;

import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class IteratorToStream<T> implements TypeTransformer<Iterator<T>, Stream<T>> {

    private IteratorToStream() { }

    @Override
    public Stream<T> call(Iterator<T> iterator) throws Exception {
        return StreamSupport.stream(((Iterable<T>)() -> iterator).spliterator(), false);
    }

    public static <T> Stream<T> apply(Iterator<T> iterator) {
        return Try.restrainGet(() -> new IteratorToStream<T>().call(iterator));
    }
}
